package b2.scene.fight.skills
{
	import b2.config.EffectConfig;
	import b2.scene.fight.PlayState;
	import b2.scene.fight.TankSprite;
	
	import com.greensock.TweenLite;
	
	import org.flixel.FlxPoint;
	
	//冲锋技能
	public class AssaultSkill extends BasicSkill
	{
		private var areaFront:int;
		private var areaBack:int;
		private var areaLeft:int;
		private var areaRight:int;
		
		public function AssaultSkill(tankId:int, tankSide:int ,level:int, skillArgs:Object)
		{
			super(tankId, tankSide ,level, skillArgs);
			var areaArgs:Object = skillArgs["areas"];
			if (areaArgs != null && areaArgs[0] != null)
			{
				areaFront = areaArgs[0].front;
				areaBack = areaArgs[0].back;
				areaLeft = areaArgs[0].left;
				areaRight = areaArgs[0].right;
			}
		}

		//技能释放
		public override function discharge(x:int, y:int, direction:int):void
		{
			//添加冲锋效果
			getPlayState().addTankEffect(tankId, tankId, EffectConfig.ASSAULT, 2, 0, 0, true);
			TweenLite.delayedCall(1, start, [x, y, direction]);
			super.discharge(x, y, direction);
		}

		private function start(x:int, y:int, direction:int):void
		{
			var skillArea:SkillArea = new SkillArea(x, y, direction);
			skillArea.addArea(areaFront, areaBack, areaLeft, areaRight);
			var playerState:PlayState = getPlayState();
			var tank:TankSprite = playerState.tanks[tankId];
			
			var toX:int = PlayState.OFFSET_X + Math.round((tank.x - PlayState.OFFSET_X) / PlayState.STEP_WIDTH) * PlayState.STEP_WIDTH;
			var	toY:int = PlayState.OFFSET_Y + Math.round((tank.y - PlayState.OFFSET_Y) / PlayState.STEP_WIDTH) * PlayState.STEP_WIDTH;
			
			if (direction == 0)
			{
				toY = findNearestGoPosition(toX, toY, -areaFront, 1);
			}
			else if (direction == 1)
			{
				toX = findNearestGoPosition(toX, toY, areaFront, 0);
			}
			else if (direction == 2)
			{
				toY = findNearestGoPosition(toX, toY, areaFront, 1);
			}
			else if (direction == 3)
			{
				toX = findNearestGoPosition(toX, toY, -areaFront, 0);
			}
			playerState.tankTweenLiteTo(tankId, 1000, direction, tank.x, tank.y, toX, toY, 1, true);
			for (var tankIdStr:String in playerState.tanks)
			{
				var otherTank:TankSprite = playerState.tanks[tankIdStr];
				if (otherTank != null && (tankId > 0 && otherTank.tankId < 0 || tankId < 0 && otherTank.tankId > 0))
				{
					if (skillArea.contain(otherTank.x + PlayState.TILE_WIDTH / 2, otherTank.y + PlayState.TILE_WIDTH / 2))
						onHit(otherTank);
				}
			}
		}
		
		//中技能的坦克
		private function onHit(tankOnHit:TankSprite):void
		{
			var areaDemage:int = level < skillArgs.areaDemage.length ? skillArgs.areaDemage[level] : skillArgs.areaDemage[0] * level;
			getPlayState().updateTankStatus(tankOnHit.tankId, tankId, tankOnHit.currentHp, -areaDemage, 0, 0, true, true);
			if (skillArgs.effectIndex != null && skillArgs.effectIndex != 0)
			{
				getPlayState().addTankEffect(tankOnHit.tankId, tankId,skillArgs.effectIndex, skillArgs.effectDuration[0], 
					skillArgs.effectValue[0], skillArgs.effectValue2[0], true);
			}
		}
		
		//找到附近可以停的点 type 为横纵轴
		private function findNearestGoPosition(x:int, y:int, moveValue:int, type:int):int
		{
			var result:int;
			var toX:int = x;
			var toY:int = y;
			if (type == 0)
				toX += moveValue;
			else
				toY += moveValue
			var i:int = 1;
			var flag:int = moveValue > 0 ? 1 : -1;
			while (isOutTile(toX, toY, type))
			{
				//为 x轴时
				if (type == 0)
					toX += i * flag * PlayState.STEP_WIDTH; 
				else
					toY += i * flag * PlayState.STEP_WIDTH; 
				flag = -flag;
				i++;
			}
			if (type == 0)
				result = toX;
			else
				result = toY;
			return result;
		}
		
		private function isOutTile(toX:int, toY:int, type:int):Boolean
		{
			if (type == 0)
				return toX > PlayState.OFFSET_X + PlayState.TILE_WIDTH * (PlayState.TILE_NUM - 1) ||
					toX < PlayState.OFFSET_X ||
					getPlayState().tileMap.overlapsPoint(new FlxPoint(toX + 1, toY + 1)) ||
					getPlayState().tileMap.overlapsPoint(new FlxPoint(toX + PlayState.TILE_WIDTH - 1, toY + 1))
			else
				return toY > PlayState.OFFSET_Y + PlayState.TILE_WIDTH * (PlayState.TILE_NUM - 1) ||
					toY < PlayState.OFFSET_Y ||
					getPlayState().tileMap.overlapsPoint(new FlxPoint(toX + 1, toY + 1)) ||
					getPlayState().tileMap.overlapsPoint(new FlxPoint(toX + 1, toY + PlayState.TILE_WIDTH - 1))
		}
	}
}